package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type ExtendConfigEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	ExtendName string // 模板名称
	ExtendType string // 设备厂商名称

	ExtendParam map[string]interface{} // 模板参数
}

type extendConfigPo struct {
	Id          int64                  `json:"id" db:"id"`                    // 对象id
	CreateTime  int64                  `json:"createTime" db:"create_time"`   // 创建时间
	UpdateTime  int64                  `json:"updateTime" db:"update_time"`   // 更新时间
	ExtendType  string                 `json:"extendType" db:"extend_type"`   // 设备厂商名称
	ExtendName  string                 `json:"extendName" db:"extend_name"`   // 设备名称
	ExtendParam map[string]interface{} `json:"extendParam" db:"extend_param"` // 配置参数
}

func (e *ExtendConfigEntity) Type() string {
	return "ExtendConfigEntity"
}

func (e *ExtendConfigEntity) TableName() string {
	return "tb_extend"
}

func (e *ExtendConfigEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *ExtendConfigEntity) Instance() *ExtendConfigEntity {
	return &ExtendConfigEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *ExtendConfigEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *ExtendConfigEntity) NewPo() interface{} {
	return &extendConfigPo{
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *ExtendConfigEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *ExtendConfigEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.ExtendName)
	return p
}

func (e *ExtendConfigEntity) BuildByJson(jsn *string) error {
	var po extendConfigPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *ExtendConfigEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *ExtendConfigEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*extendConfigPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.ExtendParam = po.ExtendParam
	ptr.ExtendType = po.ExtendType
	ptr.ExtendName = po.ExtendName
}

func (e *ExtendConfigEntity) BuildToPo() interface{} {
	ptr := e
	po := extendConfigPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.ExtendParam = ptr.ExtendParam
	po.ExtendType = ptr.ExtendType
	po.ExtendName = ptr.ExtendName

	return &po
}

// ExtendParam 私有类型
type ExtendParam struct {
	Binds  []interface{} `json:"binds"`
	Fields []ExtendField `json:"fields"`
}

// ExtendField 私有类型
type ExtendField struct {
	FieldName    string      `json:"fieldName"`
	DataType     string      `json:"dataType"`
	DefaultValue interface{} `json:"defaultValue"`
}
